package com.fundcrawler.web;

import cn.hutool.core.collection.CollectionUtil;
import com.fundcrawler.common.Constants;
import com.fundcrawler.dto.AllFundValuationDTO;
import com.fundcrawler.dto.FundValuationDTO;
import com.fundcrawler.entity.BizParam;
import com.fundcrawler.entity.FundNav;
import com.fundcrawler.service.DataService;
import com.fundcrawler.service.manager.BizParamManager;
import com.fundcrawler.service.manager.FundNavManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 默认静态资源路径（优先级从高到低）
 * classpath:/META-INF/resources/
 * classpath:/resources/
 * classpath:/static/          ← 最常用
 * classpath:/public/
 * <p>
 * 默认模板路径
 * src/main/resources/templates/
 */
@Controller
public class PageController {

    @Autowired
    private FundNavManager fundNavManager;
    @Autowired
    private BizParamManager bizParamManager;
    @Autowired
    private DataService dataService;

    @GetMapping("/")
    public String index(Model model) {
        List<BizParam> fundList = bizParamManager.findAllByParamCode(Constants.FUND_NAME);
        model.addAttribute("navs", Page.empty());
        model.addAttribute("dataSummary", dataService.getSummary());
        model.addAttribute("fundList", fundList);
        return "index";
    }

    @GetMapping("/index")
    public String index2(Model model,
                         @RequestParam String fundCode,
                         @RequestParam(defaultValue = "0") int page,
                         @RequestParam(defaultValue = "10") int size) {
        PageRequest pageable = PageRequest.of(page, size);
        Page<FundNav> navs = fundNavManager.findByFundCode(fundCode, pageable);
        List<BizParam> fundList = bizParamManager.findAllByParamCode(Constants.FUND_NAME);
        model.addAttribute("navs", navs);
        model.addAttribute("dataSummary", dataService.getSummary());
        model.addAttribute("fundList", fundList);
        return "index";
    }


    @GetMapping("/fundValuation")
    public String showQueryPage() {
        return "fundValuation";
    }

    @GetMapping("/fund/valuation")
    public String getValuationStats(
            @RequestParam String fundCode,
            @RequestParam Double unitNav,
            Model model) {

        List<FundValuationDTO> result = fundNavManager.getValuationStats(fundCode, unitNav);
        model.addAttribute("result", result);
        model.addAttribute("fundCode", fundCode);
        model.addAttribute("unitNav", unitNav);

        return "fundValuation";
    }

    @GetMapping("allValuationStats")
    public String allValuationStats(Model model) {
        List<BizParam> list = bizParamManager.findAllByParamCode(Constants.FUND_NAME);
        List<AllFundValuationDTO> result=new ArrayList<>();
        if(CollectionUtil.isNotEmpty(list)){
            for(BizParam bizParam : list){
                FundNav fundNav = fundNavManager.findLatestByFundCode(bizParam.getParamGroup());
                List<FundValuationDTO> fundValuationDTOS = fundNavManager.getValuationStats(fundNav.getFundCode(), fundNav.getUnitNav().doubleValue());
                result.add(new AllFundValuationDTO(fundNav, fundValuationDTOS));
            }
        }

        model.addAttribute("result", result);

        return "allFundValuation";
    }


    @GetMapping("/stockBoard")
    public String index() {
        return "stockBoard"; // 返回 templates/stockBoard.html
    }
}
